#include<string.h>
#include <iostream>
using std::cout;
using std::endl;
class String{
public:
    String()
    :_pstr(new char[1]())
    {
        cout << "String()" << endl;
    }
    //explicit 禁止隐式类型转换
    String(const char * pstr)
    :_pstr(new char[strlen(pstr) + 1]())
    {
        strcpy(_pstr, pstr);
        cout << "String(const char *)" << endl;
    }
    //拷贝构造函数
    String(const String & rhs)
    :_pstr(new char[strlen(rhs._pstr)+1]())
    {
        strcpy(_pstr,rhs._pstr);
        cout<<"String(const Sring & rhs)"<<endl;
    }
    //赋值构造函数
    String & operator=(const String & rhs)
    {
        //防止自赋值
        if(this == & rhs){
            return *this;
        }
        delete [] _pstr;
        _pstr = rhs._pstr;
        return *this;
    }
    //判断是否为空
    bool empty() const{
        return size() == 0;
    }
    int size() const
    {
        return strlen(_pstr);
    }
    const char * c_str() const{
        return _pstr;
    }
    ~String(){
        if(_pstr != nullptr){
            delete [] _pstr;
            _pstr = nullptr;
        }
    }
    String & append(const String & rhs){
   //申请一个空间放两个字符串
   char *temp = new char[strlen(_pstr)+strlen(rhs._pstr) + 1]();
   strcpy(temp,_pstr);
   strcat(temp,rhs._pstr);

   delete []_pstr;
   _pstr = temp;
   return *this;

    }
    
    String & append(const char * pstr){
        return append(String(pstr));
    }

private:
    char * _pstr;
};
void test(){

}
int main()
{
    test();
    return 0;
}

